using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MKDS_Course_Editor
{
    public partial class OverlayEditor : Form
    {
        string file;
        //NSMBe4.DSFileSystem.Arm9BinaryHandler arm;
        NSMBe4.DSFileSystem.NitroROMFilesystem n;
        public OverlayEditor(string file)
        {
            this.file = file;
            InitializeComponent();
        }

        private void OverlayViewer_Load(object sender, EventArgs e)
        {
            n = new NSMBe4.DSFileSystem.NitroROMFilesystem(file);
            //n.load();
            //arm = new NSMBe4.DSFileSystem.Arm9BinaryHandler(n);
            //arm.decompress();
            //arm.loadSections();
            for (int i = 0; i < n.arm9ovs.Length; i++)
            {
                listBox1.Items.Add(String.Format("Overlay {0}", i));
            }
            listBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {
                if (n.arm9ovs[listBox1.SelectedIndex].isCompressed)
                {
                    n.arm9ovs[listBox1.SelectedIndex].decompress();
                }
                File.WriteAllBytes(saveFileDialog1.FileName, n.arm9ovs[listBox1.SelectedIndex].getContents());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                //arm.sections[listBox1.SelectedIndex].data = File.ReadAllBytes(openFileDialog1.FileName);
                if (n.arm9ovs[listBox1.SelectedIndex].isCompressed)
                {
                    n.arm9ovs[listBox1.SelectedIndex].decompress();
                }
                n.arm9ovs[listBox1.SelectedIndex].beginEdit(null);
                n.arm9ovs[listBox1.SelectedIndex].replace(File.ReadAllBytes(openFileDialog1.FileName), null);
                n.arm9ovs[listBox1.SelectedIndex].endEdit(null);
                //File.WriteAllBytes(saveFileDialog1.FileName, n.arm9ovs[listBox1.SelectedIndex].getContents());
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //arm.saveSections();
            n.save();
        }
    }
}
